package team.seekers.catering.common.utils;

import team.seekers.catering.entity.params.add.FoodContentListPA;
import team.seekers.catering.entity.temp.NutrimentListDTO;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class ConvertUtil {

    public static List<FoodContentListPA> contentToContentList(byte[] content){
        //将取出的二进制数据反序列化成序列化的对象，依旧使用对象流处理
        if(content == null){
            return new ArrayList<FoodContentListPA>();
        }
        ByteArrayInputStream byteInt=new ByteArrayInputStream(content);
        ObjectInputStream objInt= null;
        try {
            objInt = new ObjectInputStream(byteInt);
            List<FoodContentListPA> o=(List<FoodContentListPA>)objInt.readObject();
            return o;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<NutrimentListDTO> ndToNutrimentList(byte[] content){
        //将取出的二进制数据反序列化成序列化的对象，依旧使用对象流处理
        if(content == null){
            return new ArrayList<NutrimentListDTO>();
        }
        ByteArrayInputStream byteInt=new ByteArrayInputStream(content);
        ObjectInputStream objInt= null;
        try {
            objInt = new ObjectInputStream(byteInt);
            List<NutrimentListDTO> o=(List<NutrimentListDTO>)objInt.readObject();
            return o;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] contentFromContentList(List<FoodContentListPA> foodContentList){
        ByteArrayOutputStream byt=new ByteArrayOutputStream();
        ObjectOutputStream obj= null;
        try {
            obj = new ObjectOutputStream(byt);
            obj.writeObject(foodContentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byt.toByteArray();
    }

    public static byte[] ndFromContentList(List<NutrimentListDTO> nutrimentList){
        ByteArrayOutputStream byt=new ByteArrayOutputStream();
        ObjectOutputStream obj= null;
        try {
            obj = new ObjectOutputStream(byt);
            obj.writeObject(nutrimentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byt.toByteArray();
    }

}
